/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projeto.voztg.pkg2.pkg0;

/**
 *
 * @author nascimento lima
 */
import java.io.File;
import java.io.IOException;


public class MbrolaSint implements Synthesizer {

    // public static final File VOICE_BR1 = new File("c:/recursos/mbrola/br1");
//    public static final File VOICE_BR1 = new File(ComponentUtils.getClearDirAbsolutePath(MbrolaSint.class.getClassLoader().getResource("projeto/voztg/pkg2/pkg0/resources/br1").getFile()));
//    public static final File VOICE_BR2 = new File(ComponentUtils.getClearDirAbsolutePath(MbrolaSint.class.getClassLoader().getResource("projeto/voztg/pkg2/pkg0/resources/br2").getFile()));
//    public static final File VOICE_BR3 = new File(ComponentUtils.getClearDirAbsolutePath(MbrolaSint.class.getClassLoader().getResource("projeto/voztg/pkg2/pkg0/resources/br3").getFile()));
//    public static final File VOICE_BR4 = new File(ComponentUtils.getClearDirAbsolutePath(MbrolaSint.class.getClassLoader().getResource("projeto/voztg/pkg2/pkg0/resources/br4").getFile()));
    public static final File VOICE_BR1 = new File("c:/recursos/mbrola/br1");
    public static final File VOICE_BR2 = new File("c:/recursos/mbrola/br2");
    public static final File VOICE_BR3 = new File("c:/recursos/mbrola/br3");
    public static final File VOICE_BR4 = new File("c:/recursos/mbrola/br4");
    private File voiceName;

    @Override
    public void setVoice(File voiceName) {
        this.voiceName = voiceName;
    }

    @Override
    public void synthesize(File inputFile, File outputFile) {  //escolha da voz default

        /*if (voiceName == null) {
         voiceName = VOICE_BR1;
         }*/

        String cmdLine = "<mbrola-bin> -e <voice> <input> <output>";
        cmdLine = cmdLine.replace("<mbrola-bin>", getPreparedFilePath(getMBrolaAppAbsolutePath()))
                .replace("<voice>", getPreparedFilePath(voiceName.getAbsolutePath()))
                .replace("<input>", getPreparedFilePath(inputFile.getAbsolutePath()))
                .replace("<output>", getPreparedFilePath(outputFile.getAbsolutePath()));

        try {

            Runtime.getRuntime().exec(cmdLine).waitFor();
        } catch (IOException e) {

            throw new RuntimeException(e);
        } catch (InterruptedException e) {

            throw new RuntimeException(e);
        }

    }

    private String getPreparedFilePath(String filePath) {
        String osName = (String) System.getProperties().get("os.name");
        if (osName.toLowerCase().indexOf("windows") > -1) {
            return "\"".concat(filePath).concat("\"");
        } else {
            return filePath.replaceAll(" ", "\\ ");
        }
    }

    private String getMBrolaAppAbsolutePath() {
        String bin = null;
        String osName = (String) System.getProperties().get("os.name");
        if (osName.toLowerCase().indexOf("windows") > -1) {
            bin = "mbrola.exe";
        } else if (osName.toLowerCase().indexOf("linux") > -1) {
            bin = "mbrola-linux-i386";
        } else if (osName.toLowerCase().indexOf("mac") > -1) {
            bin = "mbrola-darwin-ppc";
        }

        if (bin != null) {

            File caminhoMbrola = new File("c:/recursos/mbrola/" + bin);//criado para atribuir caminho externo
            return caminhoMbrola.getAbsolutePath();


        } else {
            throw new OSNotSupportedException();
        }
    }

    public File getVoice() {
        return voiceName;
    }
}
